/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * JPanelPostGrado.java
 *
 * Created on 26-11-2011, 12:13:17 AM
 */
package androidutal;

import com.toedter.calendar.JDateChooser;
import java.awt.BorderLayout;
import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.swing.JComboBox;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;

/**
 *
 * @author Roberto
 */
public class JPanelPostGrado extends javax.swing.JPanel {
    private final JVentana ventana;
    private File file;
    private String fecha;    
    private boolean isBorrador;
    private  Date date;
    
    /** Creates new form JPanelPostGrado */
    public JPanelPostGrado(JVentana ventana) {
        this.ventana=ventana;
        this.file=null;
        initComponents();
        this.isBorrador = false;
        this.date=new Date();
        this.jfecha.setDate(this.date);
    }
    public boolean isBorrador() {
        return isBorrador;
    }
    public void setIsBorrador(boolean isBorrador) {
        this.isBorrador = isBorrador;
    }
    public File getFile() {
        return file;
    }

    public void setFile(File file) {
        this.file = file;
    }

    public JTextField getjAdjunto() {
        return jAdjunto;
    }

    public void setjAdjunto(JTextField jAdjunto) {
        this.jAdjunto = jAdjunto;
    }

    public JTextField getjInstituto() {
        return jInstituto;
    }

    public void setjInstituto(JTextField jInstituto) {
        this.jInstituto = jInstituto;
    }

    public JTextField getjNombre() {
        return jNombre;
    }

    public void setjNombre(JTextField jNombre) {
        this.jNombre = jNombre;
    }

    public JTextArea getjObs() {
        return jObs;
    }

    public void setjObs(JTextArea jObs) {
        this.jObs = jObs;
    }

    public JTextArea getjRequisitos() {
        return jRequisitos;
    }

    public void setjRequisitos(JTextArea jRequisitos) {
        this.jRequisitos = jRequisitos;
    }

    public JComboBox getjTipo() {
        return jTipo;
    }

    public void setjTipo(JComboBox jTipo) {
        this.jTipo = jTipo;
    }

    public JDateChooser getJfecha() {
        return jfecha;
    }

    public void setJfecha(String fecha) {
        this.jfecha.setDate(new Date(fecha));
    }
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel4 = new javax.swing.JPanel();
        jButton6 = new javax.swing.JButton();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jNombre = new javax.swing.JTextField();
        jInstituto = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        jTipo = new javax.swing.JComboBox();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jLabel11 = new javax.swing.JLabel();
        jScrollPane2 = new javax.swing.JScrollPane();
        jRequisitos = new javax.swing.JTextArea();
        jScrollPane1 = new javax.swing.JScrollPane();
        jObs = new javax.swing.JTextArea();
        jLabel6 = new javax.swing.JLabel();
        jButton7 = new javax.swing.JButton();
        jAdjunto = new javax.swing.JTextField();
        jfecha = new com.toedter.calendar.JDateChooser();

        setBackground(java.awt.Color.black);

        jPanel4.setBackground(java.awt.Color.black);

        jButton6.setBackground(java.awt.Color.white);
        jButton6.setText("Cancelar");
        jButton6.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton6ActionPerformed(evt);
            }
        });

        jButton1.setBackground(java.awt.Color.white);
        jButton1.setText("Publicar");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jButton2.setBackground(java.awt.Color.white);
        jButton2.setText("Borrador");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel4Layout = new javax.swing.GroupLayout(jPanel4);
        jPanel4.setLayout(jPanel4Layout);
        jPanel4Layout.setHorizontalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel4Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jButton2)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 143, Short.MAX_VALUE)
                .addComponent(jButton1)
                .addGap(18, 18, 18)
                .addComponent(jButton6)
                .addContainerGap())
        );
        jPanel4Layout.setVerticalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel4Layout.createSequentialGroup()
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton1, javax.swing.GroupLayout.DEFAULT_SIZE, 23, Short.MAX_VALUE)
                    .addComponent(jButton6)
                    .addComponent(jButton2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );

        jLabel1.setForeground(new java.awt.Color(255, 255, 255));
        jLabel1.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel1.setText("Oferta Post-Grado");

        jLabel2.setForeground(java.awt.Color.white);
        jLabel2.setText("Nombre:");

        jNombre.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jNombreActionPerformed(evt);
            }
        });

        jLabel3.setForeground(java.awt.Color.white);
        jLabel3.setText("Instituto:");

        jTipo.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Diplomado","Magister", "Doctorado" }));

        jLabel4.setForeground(java.awt.Color.white);
        jLabel4.setText("Tipo:");

        jLabel5.setForeground(java.awt.Color.white);
        jLabel5.setText("Fecha:");

        jLabel11.setBackground(java.awt.Color.black);
        jLabel11.setForeground(java.awt.Color.white);
        jLabel11.setText("Requisitos:");

        jRequisitos.setColumns(20);
        jRequisitos.setLineWrap(true);
        jRequisitos.setRows(5);
        jRequisitos.setWrapStyleWord(true);
        jScrollPane2.setViewportView(jRequisitos);

        jObs.setColumns(20);
        jObs.setLineWrap(true);
        jObs.setRows(5);
        jObs.setWrapStyleWord(true);
        jScrollPane1.setViewportView(jObs);

        jLabel6.setBackground(java.awt.Color.black);
        jLabel6.setForeground(java.awt.Color.white);
        jLabel6.setText("Observaciones:");

        jButton7.setBackground(java.awt.Color.white);
        jButton7.setText("Adjuntar");
        jButton7.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton7ActionPerformed(evt);
            }
        });

        jAdjunto.setEditable(false);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel4, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, 400, Short.MAX_VALUE)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jButton7)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(jLabel6, javax.swing.GroupLayout.Alignment.TRAILING)
                        .addGroup(layout.createSequentialGroup()
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(jLabel2)
                                .addComponent(jLabel4)
                                .addComponent(jLabel3)
                                .addComponent(jLabel5))
                            .addGap(28, 28, 28)))
                    .addComponent(jLabel11))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jfecha, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 305, Short.MAX_VALUE)
                    .addComponent(jScrollPane2, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 305, Short.MAX_VALUE)
                    .addComponent(jNombre, javax.swing.GroupLayout.DEFAULT_SIZE, 305, Short.MAX_VALUE)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 305, Short.MAX_VALUE)
                    .addComponent(jTipo, javax.swing.GroupLayout.Alignment.TRAILING, 0, 305, Short.MAX_VALUE)
                    .addComponent(jInstituto, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 305, Short.MAX_VALUE)
                    .addComponent(jAdjunto, javax.swing.GroupLayout.DEFAULT_SIZE, 305, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 14, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jNombre, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jInstituto, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel3)))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel2)
                        .addGap(38, 38, 38)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel4)
                            .addComponent(jTipo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel5)
                    .addComponent(jfecha, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jAdjunto, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jButton7))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 86, Short.MAX_VALUE)
                    .addComponent(jLabel11))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel6)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 85, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel4, javax.swing.GroupLayout.PREFERRED_SIZE, 24, javax.swing.GroupLayout.PREFERRED_SIZE))
        );
    }// </editor-fold>//GEN-END:initComponents

private void jButton6ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton6ActionPerformed
// TODO add your handling code here:
    if(this.isBorrador){
        this.jButton2ActionPerformed(evt);
        this.ventana.getResumen().eliminarFila();
        if(jfecha.getDate() != null ){
            this.setVisible(false);
            ventana.add(ventana.getResumen(),BorderLayout.CENTER);
            ventana.getResumen().setVisible(true); 
        }
    }
    else{
            
            this.setVisible(false);
            ventana.add(ventana.getResumen(),BorderLayout.CENTER);
            ventana.getResumen().setVisible(true); 
    }
    this.desbloquear();
}//GEN-LAST:event_jButton6ActionPerformed

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
// TODO add your handling code here:
    if(jfecha.getDate() != null && !jNombre.getText().equals("") && !jInstituto.getText().equals("") && file!= null && 
      !jRequisitos.getText().equals("") && !jObs.getText().equals("")){
        if(this.isBorrador)
               this.ventana.getResumen().eliminarFila();
        fecha = new SimpleDateFormat("MM/dd/yyyy").format(this.jfecha.getDate()); 
        ventana.getBaseDeDatos().addPostGrado(jNombre.getText(), jInstituto.getText(), 
                (String)jTipo.getSelectedItem(), fecha, file,jRequisitos.getText(), jObs.getText(),false,this.ventana.getConfiguracionSubeYBaja().generaUrl(file.getName()));
        Date date=new Date();
        ventana.getResumen().insertarInfo((String)this.jNombre.getText(),date.toString(),"No" );
        this.setVisible(false);
        ventana.add(ventana.getResumen(),BorderLayout.CENTER);
        ventana.getResumen().setVisible(true);
        ventana.save();
        ventana.saveToWS();
        ventana.subirFTP(file);        
    }
    else{
        JOptionPane.showMessageDialog(this, "Rellene todos los campos obligatorios");
    }
}//GEN-LAST:event_jButton1ActionPerformed

private void jButton7ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton7ActionPerformed
// TODO add your handling code here:
   final JFileChooser fc = new JFileChooser();
     
     int result= fc.showOpenDialog(this);
     fc.setVisible(true);
     if (result==JFileChooser.APPROVE_OPTION)
     {
        file = fc.getSelectedFile();
        this.jAdjunto.setText(file.getPath());
     }
}//GEN-LAST:event_jButton7ActionPerformed

private void jNombreActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jNombreActionPerformed
// TODO add your handling code here:
}//GEN-LAST:event_jNombreActionPerformed

private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
// TODO add your handling code here:
//        ModeloTabla modelo=(ModeloTabla) ventana.getResumen().getTable1().getModel();
//        int fil=ventana.getResumen().getTable1().getSelectedRow();
//        ventana.getResumen().eliminarPostGrado((String)modelo.getValueAt(fil,1));
        String fecha = new SimpleDateFormat("MM/dd/yyyy").format(this.jfecha.getDate()); 
        if(file!=null)
            ventana.getBaseDeDatos().addPostGrado(jNombre.getText(), jInstituto.getText(), 
                    (String)jTipo.getSelectedItem(), fecha, file,jRequisitos.getText(), jObs.getText(),true,this.ventana.getConfiguracionSubeYBaja().generaUrl(file.getName()));
        else
            ventana.getBaseDeDatos().addPostGrado(jNombre.getText(), jInstituto.getText(), 
                    (String)jTipo.getSelectedItem(), fecha, file,jRequisitos.getText(), jObs.getText(),true,"");
        Date date=new Date();
        ventana.getResumen().insertarInfo((String)this.jNombre.getText(),this.date.toString(),"Si" );
        this.setVisible(false);
        ventana.add(ventana.getResumen(),BorderLayout.CENTER);
        ventana.getResumen().setVisible(true);
        ventana.save();
}//GEN-LAST:event_jButton2ActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTextField jAdjunto;
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton6;
    private javax.swing.JButton jButton7;
    private javax.swing.JTextField jInstituto;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel11;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JTextField jNombre;
    private javax.swing.JTextArea jObs;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JTextArea jRequisitos;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JComboBox jTipo;
    private com.toedter.calendar.JDateChooser jfecha;
    // End of variables declaration//GEN-END:variables

    public void bloquear(){
        this.getjAdjunto().setEditable(false);
        this.getJfecha().setEnabled(false);
        this.getjInstituto().setEditable(false);
        this.getjNombre().setEditable(false);
        this.getjObs().setEditable(false);
        this.getjRequisitos().setEditable(false);
        this.getjTipo().setEnabled(false);
        this.jButton1.setVisible(false);
        this.jButton2.setVisible(false);
        this.jButton7.setVisible(false);
    }
    public void desbloquear(){
        this.getjAdjunto().setEditable(true);
        this.getJfecha().setEnabled(true);
        this.getjInstituto().setEditable(true);
        this.getjNombre().setEditable(true);
        this.getjObs().setEditable(true);
        this.getjRequisitos().setEditable(true);
        this.getjTipo().setEditable(true);
        this.jButton1.setVisible(true);
        this.jButton2.setVisible(true);
        this.jButton7.setVisible(true);
    }

}
